package com.example.net.oss;

import android.util.Log;

import com.google.gson.Gson;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Long on 2019/7/26.
 */

public class ObjectStreamDownloadCallback<T> implements StreamDownLoadCallback{
    private static final String TAG = "ObjectStreamDownload";
    private T object = null;
    private Class<T> cls = null;
    public ObjectStreamDownloadCallback(Class<T> cls){
        this.cls = cls;
    }

    @Override
    public void process(InputStream is) throws IOException {
        if(is != null) {
            byte[] buffer = new byte[512];
            int len = 0;
            StringBuffer sb = new StringBuffer();
            while ((len = is.read(buffer)) > 0) {
                sb.append(new String(buffer, 0, len));
            }
            Log.i(TAG, "length = " + sb.length() + " " + sb.toString());
            Gson gson = new Gson();
            //TypeToken将T类型转化成Type
            //  Type type = new TypeToken<List<T>>(){}.getType();
            object = gson.fromJson(sb.toString(),this.cls);
        }
    }

    public T getObject(){
        return object;
    }
}
